#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <rtthread.h>
#include "rtdevice.h"

#define DBG_TAG "led.c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include "led.h"
#include "cJSON.h"
#include "sensor_pindev.h"

/*
 {
     "fan_control":0,
 }
 */
/**
 * @brief  解析收到消息
 * @param  msg_len  消息的长度
 * @param  msg 消息的内容
 * @return 无
 */
void parse_msg(uint32_t msg_len, char *msg)
{
    if (msg_len == 0)
    {
        return; // 长度不对直接返回
    }

    cJSON *obj = cJSON_Parse(msg);
    if (obj == NULL)
    {
        return; // 解析错误直接返回
    }

    cJSON *fan_ctl = cJSON_GetObjectItem(obj, "fan_control");
    if (fan_ctl != NULL)
    {
        if (fan_ctl->valueint == 0)
        {
            LOG_I("The server control message was received. fan off");
            fan_off();  // 关风扇
        }
        else if (fan_ctl->valueint == 1)
        {
            LOG_I("The server control message was received. fan on");
            fan_on();   // 开风扇
        }
    }

    if (obj != NULL)
    {
        cJSON_Delete(obj);  // 释放空间
    }

    return;
}
